package edu.ynmd.bmi.action;

import edu.ynmd.bmi.model.Stuinfo;
import edu.ynmd.bmi.service.ManageService;
import edu.ynmd.bmi.vo.BMIForm;
import edu.ynmd.bmi.vo.Result;
import edu.ynmd.bmi.vo.SFZForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class PublicAction {

    @Autowired
    private ManageService manageService;

    @PostMapping("getBMIValue")
    public Result getBMIValue(@RequestBody BMIForm bmiForm) throws Exception{
        Float resultf=bmiForm.getTz()/(bmiForm.getSg()*bmiForm.getSg());
        String resultr="";
        if (resultf<=18.4){
            resultr="偏瘦";
        }
        else if(resultf>=18&&resultf<=23.9){
            resultr="正常";
        }
        else if (resultf>=24&&resultf<=27.9){
            resultr="微胖";
        }
        else {
            resultr="肥胖";
        }

        Result i=new Result();
        i.setState(resultr);
        i.setValue(resultf);

        switch (resultr){
            case "偏瘦":
                i.setSuggest("多吃点");
                break;
            case "正常":
                i.setSuggest("不要多吃，也别少吃");
                break;
            case "微胖":
                i.setSuggest("少吃点");
                break;
            case "肥胖":
                i.setSuggest("别吃");
                break;
        }

        return i;

    }

    @GetMapping("testCrud")
    @ResponseBody
    public List<Stuinfo> testCrud() throws Exception{

        Stuinfo s=new Stuinfo();
        s.setStuinfoid("04e76227-63d0-45cb-87e4-de1285ad3a10");
        s.setXm("王二");
        s.setSfzh("530101197408290010");
        s.setCsd("云南玉溪九龙池");
        s.setCsrq("1974-08-29");
        s.setXb("男");

        return manageService.getStuinfoList();
    }

    @PostMapping("getSfzInfo")
    @ResponseBody
    public List<Stuinfo> getSfzInfo(@RequestBody SFZForm sfzForm) throws Exception{

        String xm=sfzForm.getXm();
        String sfzh=sfzForm.getSfzh();
        System.out.println("接收到的姓名是："+xm);
        System.out.println("接收到的身份证号是："+sfzh);
        String sfbm=sfzh.substring(0,2);
        System.out.println("省份字符串是："+sfbm);
        String sfname=manageService.getProvinceNameByCode(sfbm);
        System.out.println("省份字名称是："+sfname);

        String citybm=sfzh.substring(0,4);
        System.out.println("州市字符串是："+citybm);
        String cityname=manageService.getCityNameBycode(citybm);
        System.out.println("州市名称是："+cityname);
        String areabm=sfzh.substring(0,6);
        System.out.println("区县字符串是："+areabm);
        String areaname=manageService.getAreaNameByCode(areabm);
        System.out.println("区县字名称是："+areaname);

        String csrqstr=sfzh.substring(6,14);
        System.out.println("出生日期是："+csrqstr);

        String xbbm=sfzh.substring(16,17);
        System.out.println("性别字符是："+xbbm);
        String xbname=manageService.getSexNameBycode(xbbm);
        System.out.println("性别名称是:"+xbname);

        Stuinfo s=new Stuinfo();
        s.setXm(xm);
        s.setSfzh(sfzh);
        s.setCsd(sfname+cityname+areaname);
        s.setCsrq(csrqstr);
        s.setXb(xbname);
        manageService.addStuinfo(s);

        return manageService.getStuinfoList();
    }

    @PostMapping("addStuinfo")
    @ResponseBody
    public Boolean addStuinfo(@RequestBody SFZForm sfzForm) throws Exception{
        String xm=sfzForm.getXm();
        String sfzh=sfzForm.getSfzh();
        System.out.println("接收到的姓名是："+xm);
        System.out.println("接收到的身份证号是："+sfzh);
        String sfbm=sfzh.substring(0,2);
        System.out.println("省份字符串是："+sfbm);
        String sfname=manageService.getProvinceNameByCode(sfbm);
        System.out.println("省份字名称是："+sfname);

        String citybm=sfzh.substring(0,4);
        System.out.println("州市字符串是："+citybm);
        String cityname=manageService.getCityNameBycode(citybm);
        System.out.println("州市名称是："+cityname);
        String areabm=sfzh.substring(0,6);
        System.out.println("区县字符串是："+areabm);
        String areaname=manageService.getAreaNameByCode(areabm);
        System.out.println("区县字名称是："+areaname);

        String csrqstr=sfzh.substring(6,14);
        System.out.println("出生日期是："+csrqstr);

        String xbbm=sfzh.substring(16,17);
        System.out.println("性别字符是："+xbbm);
        String xbname=manageService.getSexNameBycode(xbbm);
        System.out.println("性别名称是:"+xbname);

        Stuinfo s=new Stuinfo();
        s.setXm(xm);
        s.setSfzh(sfzh);
        s.setCsd(sfname+cityname+areaname);
        s.setCsrq(csrqstr);
        s.setXb(xbname);
        return manageService.addStuinfo(s);
    }

    @PostMapping("updateStuinfo")
    @ResponseBody
    public Boolean updateStuinfo(@RequestBody SFZForm sfzForm) throws Exception{
        String xm=sfzForm.getXm();
        String sfzh=sfzForm.getSfzh();
        System.out.println("接收到的姓名是："+xm);
        System.out.println("接收到的身份证号是："+sfzh);
        String sfbm=sfzh.substring(0,2);
        System.out.println("省份字符串是："+sfbm);
        String sfname=manageService.getProvinceNameByCode(sfbm);
        System.out.println("省份字名称是："+sfname);

        String citybm=sfzh.substring(0,4);
        System.out.println("州市字符串是："+citybm);
        String cityname=manageService.getCityNameBycode(citybm);
        System.out.println("州市名称是："+cityname);
        String areabm=sfzh.substring(0,6);
        System.out.println("区县字符串是："+areabm);
        String areaname=manageService.getAreaNameByCode(areabm);
        System.out.println("区县字名称是："+areaname);

        String csrqstr=sfzh.substring(6,14);
        System.out.println("出生日期是："+csrqstr);

        String xbbm=sfzh.substring(16,17);
        System.out.println("性别字符是："+xbbm);
        String xbname=manageService.getSexNameBycode(xbbm);
        System.out.println("性别名称是:"+xbname);

        Stuinfo s=new Stuinfo();
        s.setStuinfoid(sfzForm.getStuinfoid());//修改操作必须添加主键
        s.setXm(xm);
        s.setSfzh(sfzh);
        s.setCsd(sfname+cityname+areaname);
        s.setCsrq(csrqstr);
        s.setXb(xbname);
        return manageService.updateStuinfo(s);//修改操作
    }

    @PostMapping("deleteStuinfo")
    @ResponseBody
    public Boolean deleteStuinfo(@RequestBody Stuinfo stuinfo) throws Exception {
        return manageService.deleteStuinfo(stuinfo.getStuinfoid());
    }

    @PostMapping("getSingleStuinfo")
    @ResponseBody
    public Stuinfo getSingleStuinfo(@RequestBody Stuinfo stuinfo) throws Exception {

        return manageService.getSingleStuinfo(stuinfo.getStuinfoid());
    }

    @GetMapping("getStuinfoList")
    @ResponseBody
    public List<Stuinfo> getStuinfoList() throws Exception{
        return manageService.getStuinfoList();
    }

}
